
\chapter{Operations}

\label{chapter:operations}

{\bf T} provides a simple mechanism for programming in what
has come to be known as the {\it object-oriented style\/} of programming.
Object-oriented style may be contrasted to what will here be called
{\it procedural style\/}.  In object-oriented programming, programs are
organized by giving the behavior of objects when various operations
are applied to them, whereas in procedural style, programs are organized
by giving the behavior of procedures when applied to various kinds
of objects.  These styles are equivalent in terms of computational
power, but each offers different advantages in terms of readability
and modularity.

{\bf T} supports the object-oriented style with a special kind of procedure
known as an \iix{operation}, and with forms which permit one to create
objects which exhibit certain behavior when a given operation is applied
to them.

When an operation is called, the following sequence of events occurs:
\begin{itemize}
\item[{}]A \iix{handler} is obtained for the object which was the operation's
first argument.  (Operations must always be called with at least one argument.)

\item[{}]The operation asks the handler for a \iix{method} which will handle the
operation for the object.

\item[{}]The handler either provides a method, or it indicates that the
object is not prepared to handle the operation.

\item[{}]If the handler provided a method, the method is invoked.
If not, then the operation's \iix{default method}, if any, is invoked.
If the operation has no default method, then the effect of the call to the
operation is undefined (and presumably an error condition is signalled).
\end{itemize}

In this way, an object's handler may determine how the operation
is to be performed for the object - that is, which particular method
is to be invoked as a result of invoking the operation.

Handlers map operations to methods.  Many objects may have the same
handler, or a handler may be idiosyncratic to a particular object.
However, {\it every\/} object has a handler, so all objects participate in the
generic operation dispatch protocol.

\section{Fundamental forms}


The basis of the generic operation system consists of the two special
forms {\tt OBJECT} and {\tt OPERATION}.  {\tt OBJECT}-expressions create
objects which respond appropriately to generic operations, and
{\tt OPERATION}-expressions evaluate to operations.
\index{objects}

\index{OBJECT@{\tt OBJECT}}
{\mbox {\tt (OBJECT {\it procedure\/} . {\it method-clauses\/}) $\longrightarrow$\  {\it object\/}}}
\hfill Special form

\label{OBJECT}
\begin{inset}{}

An {\tt OBJECT}-expression yields an object which is prepared to handle
generic operations according to the {\it method-clauses\/}.
In the following description, ``the object'' refers
to the value of a given {\tt OBJECT}-expression.

Each {\it method-clause\/} should be of the form
\begin{codexenv}
(({\it operation\/} . {\it variables\/}) . {\it body\/})
\end{codexenv}
{\it Operation\/} is an evaluated position, and is typically a variable
which evaluates to an operation, although it may be any
expression.  When an operation is called with the object as its first
argument, the {\it operation\/}-expressions are evaluated,
and if one yields the operation being applied to the object, the corresponding
{\it method-clause\/} is selected.  The operation is then performed
according to the selected {\it method-clause\/}: the clause's {\it variables\/}
are bound to the arguments to the operation,
and its {\it body\/}, an implicit block, is evaluated.
\begin{codexenv}
    \begin{tabular}{lll}
(DEFINE OP (OPERATION NIL)) \\
(OP (OBJECT NIL ((OP SELF) 34)))      &$\Longrightarrow$\ &  34 \\
(OP (OBJECT NIL ((OP SELF X) X)) 55)  &$\Longrightarrow$\ &  55
\end{tabular}
    \end{codexenv}

{\it Procedure\/} may be any expression, and is evaluated at the time the
{\tt OBJECT}-expression is evaluated.  The object, when called, simply
calls the value of the {\it procedure\/} expression, passing on any
arguments.  Typically {\it procedure\/} might be either a
{\tt LAMBDA}-expression, if the object is to be callable, or it is
{\tt NIL}, which by convention means that the object is not intended to
be called, the value of {\tt NIL} being an uncallable object.

In the degenerate case, where there are no method clauses, the value of
\begin{codexenv}
(OBJECT (LAMBDA {\it args\/} . {\it body\/}))
\end{codexenv}
is indistinguishable from that of
\begin{codexenv}
(LAMBDA {\it args\/} . {\it body\/}){\rm .}
\end{codexenv}

The semantics of the {\tt OBJECT} and {\tt OPERATION} special forms
can be described in terms of hypothetical primitive procedures
{\tt *OBJECT} and {\tt GET-HANDLER}.
These primitives do not actually
exist in {\bf T}, but are introduced here as expository aids.
{\tt *OBJECT} takes two arguments, and returns an object which, when
called, calls the object which was {\tt *OBJECT}'s first argument, and
when given to {\tt GET-HANDLER} returns the object which was
{\tt *OBJECT}'s second argument.  That is,
{\tt *OBJECT} creates a two-component record (like a pair),
{\tt GET-HANDLER} extracts one component, and the other component is
called when the record is called.
\begin{codexenv}
    \begin{tabular}{lll}
(GET-HANDLER (*OBJECT {\it proc handler\/}))   &$\equiv$&  {\it handler\/} \\
((*OBJECT {\it proc\/} {\it handler\/}) {\it arg\/} \ldots\ )   &$\equiv$&  ({\it proc\/} {\it arg\/} \ldots\ )
\end{tabular}
    \end{codexenv}
In addition, {\tt GET-HANDLER} is defined on {\it all\/} objects to return
some handler, even objects not created by {\tt *OBJECT} (if indeed
there are any such objects).

Given these primitives, the following rough equivalence holds:
{\tt \begin{tabbing}
(OBJECT \= {\it proc\/} \+ \\
        (({\it op$_{\hbox{1}}$\/} . {\it args$_{\hbox{1}}$\/}) . {\it body$_{\hbox{1}}$\/}) \\
        (({\it op$_{\hbox{2}}$\/} . {\it args$_{\hbox{2}}$\/}) . {\it body$_{\hbox{2}}$\/}) \\
        \ldots\  \\
        (({\it op$_{\hbox{n}}$\/} . {\it args$_{\hbox{n}}$\/}) . {\it body$_{\hbox{n}}$\/})) \\
  $\equiv$ \- \\
(*OBJECT \= {\it proc\/} \+ \\
         (LAMBDA \= (OP) \+ LNL
           (SELECT \= OP \+ \\
             (({\it op$_{\hbox{1}}$\/}) (LAMBDA {\it args$_{\hbox{1}}$\/} . {\it body$_{\hbox{1}}$\/})) \\
             (({\it op$_{\hbox{2}}$\/}) (LAMBDA {\it args$_{\hbox{2}}$\/} . {\it body$_{\hbox{2}}$\/})) \\
             \ldots\  \\
             (({\it op$_{\hbox{n}}$\/}) (LAMBDA {\it args$_{\hbox{n}}$\/} . {\it body$_{\hbox{n}}$\/})) \\
             (ELSE NIL))))
\end{tabbing}}
The outer {\tt LAMBDA}-expression yields the object's handler;
the inner {\tt LAMBDA}-expressions yield the methods, and
the mapping from operations to methods is accomplished by
the {\tt SELECT}-expression (see {\tt SELECT}, page \pageref{SELECT}).
Note that the syntactic positions
{\it op$_{\hbox{1}}$\/},
{\it op$_{\hbox{2}}$\/},
\ldots\ 
{\it op$_{\hbox{n}}$\/}
are evaluated positions, and the operation expressions
are evaluated when an operation is applied to the object,
not when the object is created.
\end{inset}

\index{OPERATION@{\tt OPERATION}}
{\mbox {\tt (OPERATION {\it default\/} . {\it method-clauses\/}) $\longrightarrow$\  {\it operation\/}}}
\hfill Special form

\begin{inset}{}

The syntax of {\tt OPERATION} is the same as that of {\tt OBJECT}, but its
semantics and application are somewhat different.  An
{\tt OPERATION}-expression evaluates to an operation.  When called, the
operation obtains a handler for its first argument, calls the handler to
obtain a method, and then invokes the method.  The default method for
the operation is established as being {\it default\/}.

As the subject of another generic operation, an operation is an object
like any other, and in this case the operation acts just as if it had
been created by an {\tt OBJECT}-expression with the same {\it method-clauses\/}.
In this way one can establish the behavior of an operation
when subject to other operations, for example {\tt SETTER}.

The following rough equivalence describes the semantics of {\tt OPERATION}.
Some details have been omitted.
{\tt \begin{tabbing}
(OPERATION {\it default\/} . {\it methods\/}) \\
  $\equiv$ \\
(LABELS ((OP (OBJECT (LAMBDA \= (OBJ . ARGS) \+ \\
                       (LET \= ((METHOD ((GET-HANDLER OBJ) OP))) \+ \\
                         (COND \= (METHOD \+ \\
                                (APPLY METHOD OBJ ARGS)) \\
                               (ELSE \\
                                (APPLY {\it default\/} OBJ ARGS))))) \- \\
                     . {\it methods\/}))) \\
  OP)
\end{tabbing}}
For example:
\begin{codexenv}
    \begin{tabular}{lll}
(DEFINE OP (OPERATION (LAMBDA (OBJ) 'ZEBU))) \\
(OP (OBJECT NIL ((OP SELF) 'QUAGGA)))  &$\Longrightarrow$\ &  QUAGGA \\
(OP 'ELAND)                            &$\Longrightarrow$\ &  ZEBU
\end{tabular}
    \end{codexenv}
An operation is created, and the variable {\tt OP} is bound to it.
The operation's default method always returns the symbol {\tt ZEBU}.
When the operation is applied to the value of the {\tt OBJECT}-expression,
the appropriate method is invoked, and the call to the operation yields
the symbol {\tt QUAGGA}.  When the operation is applied to an object
which doesn't handle it - the symbol {\tt ELAND} - the operation's
default method is invoked, so the call yields the symbol {\tt ZEBU}.
\end{inset}

\index{OPERATION?@{\tt OPERATION?}}
{\mbox {\tt (OPERATION? {\it object\/}) $\longrightarrow$\  {\it boolean\/}}}
\hfill Type predicate

\begin{inset}{}
Returns true if {\it object\/} is an operation.
\end{inset}

\section{Defining operations}


\index{DEFINE-OPERATION@{\tt DEFINE-OPERATION}}
{\mbox {\tt (DEFINE-OPERATION ({\it variable\/} . {\it argument-vars\/}) . {\it body\/}) $\longrightarrow$\  {\it undefined\/}}}
\hfill Special form

\begin{inset}{}

Defines {\it variable\/} to be an operation.
The syntax is intended to be analogous to that of {\tt DEFINE}.
The operation's default method is defined by {\it argument-vars\/} and {\it body\/}.
If there is no {\it body\/}, then the operation's default method
is undefined.  In this case, the {\it argument-vars\/}
appear only for documentary purposes.
\begin{codexenv}
(DEFINE-OPERATION ({\it var\/} . {\it args\/}) . {\it body\/}) \\
  $\equiv$  (DEFINE {\it var\/} (OPERATION (LAMBDA {\it args\/} . {\it body\/}))) \\

(DEFINE-OPERATION ({\it var\/} . {\it args\/})) \\
  $\equiv$  (DEFINE {\it var\/} (OPERATION UNDEFINED-EFFECT))
\end{codexenv}
\end{inset}

\index{DEFINE-SETTABLE-OPERATION@{\tt DEFINE-SETTABLE-OPERATION}}
{\mbox {\tt (DEFINE-SETTABLE-OPERATION ({\it variable\/} . {\it argument-vars\/}) . {\it body\/}) $\longrightarrow$\  {\it undefined\/}}}
\hfill Special form

\label{DEFINE-SETTABLE-OPERATION}
\begin{inset}{}

Defines {\it variable\/} to be an operation, as with {\tt DEFINE-OPERATION},
but arranges for the operation's ``setter'' to be another operation,
so that the operation is ``settable'' (see page \pageref{Settable}).
{\tt \begin{tabbing}
(DEFINE-SETTABLE-OPERATION \= ({\it var\/} . {\it args\/}) . {\it body\/}) \\
  \> $\equiv$ \\
(DEFINE \= {\it var\/} \+ \\
  (LET \= ((THE-SETTER (OPERATION UNDEFINED-EFFECT))) \+ \\
    (OPERATION \= (LAMBDA {\it args\/} . {\it body\/}) \+ \\
      ((SETTER SELF) THE-SETTER))))
\end{tabbing}}
\end{inset}

\index{DEFINE-PREDICATE@{\tt DEFINE-PREDICATE}}
{\mbox {\tt (DEFINE-PREDICATE {\it variable\/}) $\longrightarrow$\  {\it undefined\/}}}
\hfill Special form

\begin{inset}{}

Defines {\it variable\/} to be an operation which, by default,
returns false.
\begin{codexenv}
(DEFINE-PREDICATE {\it var\/}) \\
  $\equiv$ \\
(DEFINE-OPERATION ({\it var\/} OBJ) NIL)
\end{codexenv}

The intent is that particular {\tt OBJECT}-expressions contain clauses
of the form {\mbox {\tt (({\it variable\/} SELF) T)}}.  This way the operation
defined by {\tt DEFINE-PREDICATE} may act as a type predicate that returns
true only for those objects returned by such {\tt OBJECT}-expressions.
\end{inset}


\section{Example}


Hypothetical implementation of {\tt CONS}:

{\tt \begin{tabbing}
(DEFINE-PREDICATE PAIR?) \\
(DEFINE-SETTABLE-OPERATION (CAR PAIR)) \\
(DEFINE-SETTABLE-OPERATION (CDR PAIR)) \\

(DEFINE \= (CONS THE-CAR THE-CDR) \+ \\
  (OBJECT \= NIL \+ \\
          ((PAIR? SELF) T) \\
          ((CAR SELF) THE-CAR) \\
          ((CDR SELF) THE-CDR) \\
          (((SETTER CAR) SELF NEW-CAR) (SET THE-CAR NEW-CAR)) \\
          (((SETTER CDR) SELF NEW-CDR) (SET THE-CDR NEW-CDR))))
\end{tabbing}}

